package cn.tedu.medicalmanagementsystem.base.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("${file.upload-dir}")
    private String uploadDir;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//**表示在后端允许匹配客户端发过来的任意请求
                .allowedHeaders("*")//任意请求头都可以
                .allowedMethods("*")//任意请求方法都可以
                .allowedOrigins("http://localhost:9090")
                .allowedOriginPatterns("*")//任意域都可以
                .allowCredentials(true)//请求可以携带会话相关信息（cookie/session）
                .maxAge(3600);//同一请求一小时内不在检测，直接放行

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:" + uploadDir + "/");
    }
}
